'use strict';

/**
 * @file 解决浮动运算问题，避免小数点后产生多位数和计算精度损失。
 * 问题示例：2.3 + 2.4 = 4.699999999999999，1.0 - 0.9 = 0.09999999999999998
 */
let numberPrecision = {

    /**
     * 把错误的数据转正
     * strip(0.09999999999999998)=0.1
     */
    strip: function (num, precision) {
        if (precision === void 0) {
            precision = 12;
        }
        return +parseFloat(num.toPrecision(precision));
    }
    /**
     * 精确加法
     */
    , plus: function plus(num1, num2) {
        var others = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            others[_i - 2] = arguments[_i];
        }
        if (others.length > 0) {
            return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1)));
        }
        var baseNum = Math.pow(10, Math.max(numberPrecision.digitLength(num1), numberPrecision.digitLength(num2)));
        return (numberPrecision.times(num1, baseNum) + numberPrecision.times(num2, baseNum)) / baseNum;
    }
    /**
     * 精确减法
     */
    , minus: function minus(num1, num2) {
        var others = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            others[_i - 2] = arguments[_i];
        }
        if (others.length > 0) {
            return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1)));
        }
        var baseNum = Math.pow(10, Math.max(numberPrecision.digitLength(num1), numberPrecision.digitLength(num2)));
        return (numberPrecision.times(num1, baseNum) - numberPrecision.times(num2, baseNum)) / baseNum;
    }
    /**
     * 精确乘法
     */
    , times: function times(num1, num2) {
        var others = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            others[_i - 2] = arguments[_i];
        }
        if (others.length > 0) {
            return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
        }
        var num1Changed = numberPrecision.float2Fixed(num1);
        var num2Changed = numberPrecision.float2Fixed(num2);
        var baseNum = numberPrecision.digitLength(num1) + numberPrecision.digitLength(num2);
        var leftValue = num1Changed * num2Changed;
        numberPrecision.checkBoundary(leftValue);
        return leftValue / Math.pow(10, baseNum);
    }
    /**
     * 精确除法
     */
    , divide: function divide(num1, num2) {
        var others = [];
        for (var _i = 2; _i < arguments.length; _i++) {
            others[_i - 2] = arguments[_i];
        }
        if (others.length > 0) {
            return divide.apply(void 0, [divide(num1, num2), others[0]].concat(others.slice(1)));
        }
        var num1Changed = numberPrecision.float2Fixed(num1);
        var num2Changed = numberPrecision.float2Fixed(num2);
        numberPrecision.checkBoundary(num1Changed);
        numberPrecision.checkBoundary(num2Changed);
        return numberPrecision.times((num1Changed / num2Changed), Math.pow(10, numberPrecision.digitLength(num2) - numberPrecision.digitLength(num1)));
    }
    /**
     * 四舍五入
     */
    , round: function round(num, ratio) {
        var base = Math.pow(10, ratio);
        return numberPrecision.divide(Math.round(numberPrecision.times(num, base)), base);
    }
    /**
     * 检测数字是否越界，如果越界给出提示
     * @param num 输入数 {*number}
     */
    , checkBoundary: function checkBoundary(num) {
        if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
            console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
        }
    }
    /**
     * 把小数转成整数，支持科学计数法。如果是小数则放大成整数
     * @param num 输入数 {*number}
     */
    , float2Fixed: function float2Fixed(num) {
        if (num.toString().indexOf('e') === -1) {
            return Number(num.toString().replace('.', ''));
        }
        var dLen = numberPrecision.digitLength(num);
        return dLen > 0 ? num * Math.pow(10, dLen) : num;
    }
    /**
     * Return digits length of a number
     * @param num Input number {*number}
     */
    , digitLength: function digitLength(num) {
        // Get digit length of e
        var eSplit = num.toString().split(/[eE]/);
        var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
        return len > 0 ? len : 0;
    }
};

